package com.zhz.employee.controller;

import com.zhz.common.controller.BaseController;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.tool.mybatisplus.base.core.contract.R;
import com.zhz.employee.biz.EmployeeBizService;
import com.zhz.employee.contract.request.*;
import com.zhz.employee.contract.response.*;
import com.zhz.model.employee.EmployeeArchive;
import com.zhz.model.employee.contract.param.EmployeeArchiveQueryParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 员工控制器
 *
 * @author zhouhengzhe
 */
@RestController
@CrossOrigin
@RequestMapping("/employees")
public class EmployeeController extends BaseController {

    private final EmployeeBizService employeeBizService;

    @Autowired
    public EmployeeController(EmployeeBizService employeeBizService) {
        this.employeeBizService = employeeBizService;
    }

    /**
     * 打印员工pdf报表 get /{id}/pdf
     */
    @PostMapping("/printPdf")
    @ApiOperation("打印员工pdf报表")
    public R<Void> printPdf(@RequestBody @Validated EmployeePdfRequest employeePdfRequest) {
        employeeBizService.printPdf(employeePdfRequest, response);
        return R.ok();
    }

    /**
     * 员工个人信息保存 /{id}/personalInfo
     * <p>
     * name = "id") String uid, @RequestBody Map map
     */
    @PostMapping("/savePersonalInfo")
    @ApiOperation("员工个人信息保存")
    public R<Void> savePersonalInfo(@RequestBody EmployeeSavePersonalRequest employeeSavePersonalRequest) {
        employeeBizService.savePersonalInfo(employeeSavePersonalRequest, companyId);
        return R.ok();
    }

    /**
     * 员工个人信息读取
     * /{id}/personalInfo
     * （name = "id") String uid
     */
    @PostMapping("/findPersonalInfo")
    @ApiOperation("员工个人信息读取")
    public R<FindPersonalInfoResponse> findPersonalInfo(@RequestBody @Validated FindPersonalInfoRequest findPersonalInfoRequest) {
        return R.ok(employeeBizService.findPersonalInfo(findPersonalInfoRequest));
    }

    /**
     * 员工岗位信息保存
     * /{id}/jobs
     * <p>
     * name = "id" String uid, @RequestBody UserCompanyJobs sourceInfo
     */
    @PostMapping("/saveJobsInfo")
    @ApiOperation("员工岗位信息保存")
    public R<Void> saveJobsInfo(@RequestBody EmployeeSaveJobsInfoRequest employeeSaveJobsInfoRequest) {
        employeeBizService.saveJobsInfo(employeeSaveJobsInfoRequest, companyId);
        return R.ok();
    }

    /**
     * 员工岗位信息读取
     * /{id}/jobs
     * name = "id" String uid
     */
    @PostMapping("/findJobsInfo")
    @ApiOperation("员工岗位信息读取")
    public R<EmployeeFindInfoResponse> findJobsInfo(@RequestBody EmployeeFindJobsInfoRequest employeeFindJobsInfoRequest) {
        return R.ok(employeeBizService.findJobsInfo(employeeFindJobsInfoRequest, companyId));
    }

    /**
     * 离职表单保存
     * name = "id") String uid, @RequestBody EmployeeResignation resignation
     */
    @PostMapping(value = "/saveLeave")
    @ApiOperation("离职表单保存")
    public R<Void> saveLeave(@RequestBody EmployeeSaveLeaveRequest employeeSaveLeaveRequest) {
        employeeBizService.saveLeave(employeeSaveLeaveRequest);
        return R.ok();
    }

    /**
     * 离职表单读取
     * value = "/{id}/leave"
     * <p>
     * name = "id") String uid
     */
    @PostMapping("/findLeave")
    @ApiOperation("离职表单读取")
    public R<EmployeeFindLeaveResponse> findLeave(@RequestBody EmployeeFindLeaveRequest employeeFindLeaveRequest) {
        return R.ok(employeeBizService.findLeave(employeeFindLeaveRequest));
    }


    /**
     * 调岗表单保存
     * /{id}/transferPosition
     * String uid, @RequestBody EmployeeTransferPosition transferPosition
     */
    @PostMapping("/saveTransferPosition")
    @ApiOperation("调岗表单保存")
    public R<Void> saveTransferPosition(@RequestBody EmployeeTransferPositionRequest employeeTransferPositionRequest) {
        employeeBizService.saveTransferPosition(employeeTransferPositionRequest);
        return R.ok();
    }

    /**
     * 调岗表单读取
     * /{id}/transferPosition
     */
    @PostMapping("/findTransferPosition")
    @ApiOperation("调岗表单读取")
    public R<EmployeeFindInfoResponse> findTransferPosition(@RequestBody EmployeeFindTransferPositionRequest employeeFindTransferPositionRequest) {
        return R.ok(employeeBizService.findTransferPosition(employeeFindTransferPositionRequest));
    }


    /**
     * /{id}/positive
     * String uid, @RequestBody EmployeePositive positive
     * 转正表单保存
     */
    @PostMapping("/savePositive")
    @ApiOperation("转正表单保存")
    public R<Void> savePositive(@RequestBody EmployeePositiveSaveRequest employeePositiveSaveRequest) {
        employeeBizService.savePositive(employeePositiveSaveRequest);
        return R.ok();
    }

    /**
     * 转正表单读取
     * /{id}/positive
     */
    @PostMapping("/findPositive")
    @ApiOperation("转正表单读取")
    public R<EmployeeFindPositiveResponse> findPositive(@RequestBody EmployeeFindPositiveRequest employeeFindPositiveRequest) {
        return R.ok(employeeBizService.findPositive(employeeFindPositiveRequest));
    }

    /**
     * 历史归档列表
     */
    @PostMapping(value = "/archives")
    @ApiOperation("历史归档列表")
    public R<Page<EmployeeArchiveResponse>> findArchives(@RequestBody EmployeeArchiveQueryParam employeeArchiveQueryParam) {
        return R.ok(employeeBizService.findArchives(employeeArchiveQueryParam));
    }
//
////    /**
////     * 当月人事报表导出
////     *  参数：
////     *      年月-月（2018-02%）
////     */
////    @RequestMapping(value = "/export/{month}", method = RequestMethod.GET)
////    public void export(@PathVariable String month) {
////        //1.获取报表数据
////        List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId,month);
////        //2.构造Excel
////        //创建工作簿
////        Workbook wb = new XSSFWorkbook();
////        //构造sheet
////        Sheet sheet = wb.createSheet();
////        //创建行
////        //标题
////        String [] titles = "编号,姓名,手机,最高学历,国家地区,护照号,籍贯,生日,属相,入职时间,离职类型,离职原因,离职时间".split(",");
////        //处理标题
////
////        Row row = sheet.createRow(0);
////
////        int titleIndex=0;
////        for (String title : titles) {
////            Cell cell = row.createCell(titleIndex++);
////            cell.setCellValue(title);
////        }
////
////        int rowIndex = 1;
////        Cell cell=null;
////        for (EmployeeReportResult employeeReportResult : list) {
////            row = sheet.createRow(rowIndex++);
////            // 编号,
////            cell = row.createCell(0);
////            cell.setCellValue(employeeReportResult.getUserId());
////            // 姓名,
////            cell = row.createCell(1);
////            cell.setCellValue(employeeReportResult.getUsername());
////            // 手机,
////            cell = row.createCell(2);
////            cell.setCellValue(employeeReportResult.getMobile());
////            // 最高学历,
////            cell = row.createCell(3);
////            cell.setCellValue(employeeReportResult.getTheHighestDegreeOfEducation());
////            // 国家地区,
////            cell = row.createCell(4);
////            cell.setCellValue(employeeReportResult.getNationalArea());
////            // 护照号,
////            cell = row.createCell(5);
////            cell.setCellValue(employeeReportResult.getPassportNo());
////            // 籍贯,
////            cell = row.createCell(6);
////            cell.setCellValue(employeeReportResult.getNativePlace());
////            // 生日,
////            cell = row.createCell(7);
////            cell.setCellValue(employeeReportResult.getBirthday());
////            // 属相,
////            cell = row.createCell(8);
////            cell.setCellValue(employeeReportResult.getZodiac());
////            // 入职时间,
////            cell = row.createCell(9);
////            cell.setCellValue(employeeReportResult.getTimeOfEntry());
////            // 离职类型,
////            cell = row.createCell(10);
////            cell.setCellValue(employeeReportResult.getTypeOfTurnover());
////            // 离职原因,
////            cell = row.createCell(11);
////            cell.setCellValue(employeeReportResult.getReasonsForLeaving());
////            // 离职时间
////            cell = row.createCell(12);
////            cell.setCellValue(employeeReportResult.getResignationTime());
////        }
////        //3.完成下载
////        ByteArrayOutputStream os = new ByteArrayOutputStream();
////        wb.write(os);
////        new DownloadUtils().download(os,response,month+"人事报表.xlsx");
////    }
//
//    /**
//     * 采用模板打印的形式完成报表生成
//     * 模板
//     * 参数：
//     * 年月-月（2018-02%）
//     */
//    @RequestMapping(value = "/export/{month}", method = RequestMethod.GET)
//    public void export(@PathVariable String month) {
//        //1.获取报表数据
//        List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId, month);
//
//        //2.加载模板
//        Resource resource = new ClassPathResource("excel-template/hr-demo.xlsx");
//        FileInputStream fis = new FileInputStream(resource.getFile());
//
//        //3.根据模板创建工作簿
//        Workbook wb = new XSSFWorkbook(fis);
//        //4.读取工作表
//        Sheet sheet = wb.getSheetAt(0);
//        //5.抽取公共样式
//        Row row = sheet.getRow(2);
//        CellStyle styles[] = new CellStyle[row.getLastCellNum()];
//        for (int i = 0; i < row.getLastCellNum(); i++) {
//            Cell cell = row.getCell(i);
//            styles[i] = cell.getCellStyle();
//        }
//        //6.构造单元格
//        int rowIndex = 2;
//        Cell cell = null;
//        for (EmployeeReportResult employeeReportResult : list) {
//            row = sheet.createRow(rowIndex++);
////            for(int j=0;j<styles.length;j++) {
////                cell = row.createCell(j);
////                cell.setCellStyle(styles[j]);
////            }
//
//            // 编号,
//            cell = row.createCell(0);
//            cell.setCellValue(employeeReportResult.getUserId());
//            cell.setCellStyle(styles[0]);
//            // 姓名,
//            cell = row.createCell(1);
//            cell.setCellValue(employeeReportResult.getUsername());
//            cell.setCellStyle(styles[1]);
//            // 手机,
//            cell = row.createCell(2);
//            cell.setCellValue(employeeReportResult.getMobile());
//            cell.setCellStyle(styles[2]);
//            // 最高学历,
//            cell = row.createCell(3);
//            cell.setCellValue(employeeReportResult.getTheHighestDegreeOfEducation());
//            cell.setCellStyle(styles[3]);
//            // 国家地区,
//            cell = row.createCell(4);
//            cell.setCellValue(employeeReportResult.getNationalArea());
//            cell.setCellStyle(styles[4]);
//            // 护照号,
//            cell = row.createCell(5);
//            cell.setCellValue(employeeReportResult.getPassportNo());
//            cell.setCellStyle(styles[5]);
//            // 籍贯,
//            cell = row.createCell(6);
//            cell.setCellValue(employeeReportResult.getNativePlace());
//            cell.setCellStyle(styles[6]);
//            // 生日,
//            cell = row.createCell(7);
//            cell.setCellValue(employeeReportResult.getBirthday());
//            cell.setCellStyle(styles[7]);
//            // 属相,
//            cell = row.createCell(8);
//            cell.setCellValue(employeeReportResult.getZodiac());
//            cell.setCellStyle(styles[8]);
//            // 入职时间,
//            cell = row.createCell(9);
//            cell.setCellValue(employeeReportResult.getTimeOfEntry());
//            cell.setCellStyle(styles[9]);
//            // 离职类型,
//            cell = row.createCell(10);
//            cell.setCellValue(employeeReportResult.getTypeOfTurnover());
//            cell.setCellStyle(styles[10]);
//            // 离职原因,
//            cell = row.createCell(11);
//            cell.setCellValue(employeeReportResult.getReasonsForLeaving());
//            cell.setCellStyle(styles[11]);
//            // 离职时间
//            cell = row.createCell(12);
//            cell.setCellValue(employeeReportResult.getResignationTime());
//            cell.setCellStyle(styles[12]);
//        }
//        //7.下载
//        //3.完成下载
//        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        wb.write(os);
//        new DownloadUtils().download(os, response, month + "人事报表.xlsx");
//    }
}



